<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%@ include file="/WEB-INF/taglib.jsp"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/strict.dtd">
<meta name="renderer" content="ie-comp">
<html>
  <head>
	<title>字典信息</title>
	<%@ include file="/WEB-INF/jsp/include.jsp"%>
	<script type="text/javascript">
		var path = "${path}";

        var codeFlag = 0;
        var nameFlag = 0;
		$(function() {
			setValue();
	        var grid=$("#tt");
			initGrid(grid);
			getTypeById();
		});
		
		
		
		//获取父页面的值
		function setValue(){
			var treeObj = parent.$.fn.zTree.getZTreeObj("treeDemo");
			var nodes = treeObj.getSelectedNodes(); 
			 $("#parentDictId").val(nodes[0].id);
		}

        /**
		 * 查询类型
         */
        function getTypeById(){
            var treeObj = parent.$.fn.zTree.getZTreeObj("treeDemo");
            var nodes = treeObj.getSelectedNodes();
            var id=nodes[0].id;
            if(id!="0"){
                var param = new Object();
                param.dictId =id;
                $.ajax({
                    type:"POST",
                    url:path+'/system/dict/returnSingleDictDto',
                    data: param,
                    dataType:"json",
                    async: false,
                    success:function(data){
                        if(data.code != '0000'){
                            //alert('获取数据时发生错误：' + data.msg);
                        } else {
                            var dto = data.returnObject;
                            $("#dictType").val(dto.dictCode);

                        }
                    },
                    error: function() {
                        alert('获取数据时发生错误！');
                    }
                });
            }
        }
		
        //code 查重复
        function checkRepeatCode(value){
            var params = new Object();
            var type=$("#dictType").val();
            params.dictCode = value;
            params.dictType = type;
            $.ajax({
                type: "POST",
                url: '${path}/system/dict/returnSingleDictDto',
                dataType: "json",
                data: params,
                success: function callback(data){
                    if(data.code=="0001"){
                        codeFlag = 0;
                    }else{
                        codeFlag = 1;
                        $.messager.alert('操作提示','代码不可用','info',function(){
                            $('#dictCode')[0].focus();
                        });
                    }
                },
                error:function(){

                }
            });
        }

        //name 查重复
        function checkRepeatName(value){
            var params = new Object();
            var type=$("#dictType").val();
            params.dictName = value;
            params.dictType = type;

            $.ajax({
                type: "POST",
                url: '${path}/system/dict/returnSingleDictDto',
                dataType: "json",
                data: params,
                success: function callback(data){
                    if(data.code=="0001"){
                        nameFlag = 0;
                    }else{
                        nameFlag = 1;
                        $.messager.alert('操作提示','名称不可用','info',function(){
                            $('#dictName')[0].focus();
                        });
                    }
                },
                error:function(){

                }
            });
        }
		
			//保存增加的数据
		 function btnSubmitAdd(flag) {
             if(codeFlag == 1){
                 checkRepeatCode($('#dictCode').val());
                 return;
             }
             if(nameFlag == 1){
                 checkRepeatName($('#dictName').val());
                 return;
             }
             if (!validform("dictForm")) {
                 return;
             }
			//var param = getFormValue();
			$.ajax({
	 			type:"POST",  
				url:path+'/system/dict/save',
				data: $('#dictForm').serialize(),
				dataType:"json",  
				async: false,  
				success:function(data){	
					if(data.code != '0000'){
	     				alert('保存数据时发生错误：' + data.msg);
	     			} else {
	     				refreshNode();
	     				parent.addRefreshGrid();
	     				closeLayer();	
	     				
	    			}
				},
				error: function() {
					alert('保存数据时发生错误！');
				}
			 });
		    }
	    
		function refreshNode(){
			var name=$("#dictName").val();
			var type=$("#dictType").val();
			var param = new Object();
			param.dictName = name;
			param.dictType = type;
			$.ajax({
	 			type:"POST",
				url:path+'/system/dict/returnSingleDictDto',
				data: param,
				dataType:"json",
				async: false,
				success:function(data){
					if(data.code != '0000'){
//	     				alert('获取数据时发生错误：' + data.msg);
	     			} else {
	     				var dto = data.returnObject;
	     				//增加节点
	     				var treeObj =parent. $.fn.zTree.getZTreeObj("treeDemo");
	     				var nodes = treeObj.getSelectedNodes();
	     				var newNode = {name:$("#dictName").val(),
	     						         id:dto.dictId};
	     				newNode = treeObj.addNodes(nodes[0], newNode);
	    			}
				},
				error: function() {
					alert('获取数据时发生错误！');
				}
			});
		}
	  
	    function btnCancelAdd() {
	    	closeLayer();
		}
	    
	 </script>
  </head>

  <body  style="margin:5px; padding:0px;" > 
 
	<%@ include file="/WEB-INF/jsp/loading.jsp" %>
	<div class="easyui-layout" fit="true" >
		<div id="AddDlgDiv" align="center">
			<form id="dictForm" method="post" >
		    <table  class="f_table" cellspacing="1" align="center" >
						<tr>
							<td align="right" class="s_bg" style="width:70px">编码：</td>
							<td class="s_bg" style="width: 200px;height:25px;"><input type="text" id="dictCode" name="dictCode" class="easyui-validatebox" onchange="checkRepeatCode(this.value);" data-options="required:true" style="width: 100%;height:100%;"/></td>
							<td align="right" class="s_bg" style="width:70px;display:none">上级代码：</td>
							<td class="s_bg" style="width: 200px;height:25px;display:none;"><input type="text" id="parentDictId" name="parentDictId" value="0" readonly="readonly" class="easyui-validatebox" data-options="required:true" style="width:100%;height:100%;"/></td>
						    <td align="right" class="s_bg" style="width:70px">名称：</td>
							<td class="s_bg" style="width: 200px;height:25px;"><input type="text" id="dictName" name ="dictName" class="easyui-validatebox" onchange="checkRepeatName(this.value);"  data-options="required:true,tipPosition:'left'" style="width:100%;height:100%;"/></td>
						</tr>
						<tr>
						    <td align="right"  class="s_bg" style="width:70px">描述：</td>
							<td class="s_bg"  style="width: 200px;height:25px;"><input type="text" id="dictDescription" name="dictDescription" class="easyui-validatebox" data-options="required:true" style="width:100%;height:100%;"/></td>
						    <td align="right" class="s_bg" style="width:70px">类型：</td>
							<td class="s_bg" style="width: 200px;height:25px;"><input type="text" id="dictType" name="dictType" readonly="readonly" class="easyui-validatebox" data-options="required:true,tipPosition:'left'" value="0" style="width:100%;height:100%;"/></td>	
						    </tr>
						<tr>
				            <td align="right" class="s_bg" style="width:70px">排序：</td>
							<td class="s_bg" style="width: 200px;height:25px;"><input type="text" id="sortby" name="sortby" class="easyui-validatebox" data-options="required:true"  style="width: 100%;height:100%;"/></td>	
                            <td align="right" class="s_bg" style="width:70px"></td>
                            <td class="s_bg" style="width: 200px;height:25px;"></td>
							<input type="hidden" id="delMark" name="delMark" value="0" />
							<input type="hidden" id="authMark"  name="authMark" value=""/>
							<input type="hidden" id="dictId"  name="dictId" value=""/>
						</tr>
			</table>
			</form>  
			  <div align="center" style="margin-top: 15px;">
		        	<a href="javascript:void(0)" class="easyui-linkbutton" iconcls="icon-ok" onclick="btnSubmitAdd()">确定</a>
		        	<a href="javascript:void(0)" class="easyui-linkbutton" iconcls="icon-cancel" onclick="btnCancelAdd();">取消</a>
       		 </div>
		</div>
	</div>
</body>
</html>
